package com.yonyou.jrmob.contacts.contact.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.contact.bean.IndexDetail_ct;
import com.yonyou.jrmob.contacts.contact.utils.CSIMCardUtil_ct;
import com.yonyou.jrmob.contacts.contact.view.dialog.MaterialDialog;

import java.util.List;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;

/**
 * Created by Administrator on 2016/7/13.
 */
public class UsefulTelAdapter_ct extends BaseAdapter {
    Context context;
    private List<IndexDetail_ct> indexDetailCts;
    private boolean hasSIMCard;

    public UsefulTelAdapter_ct(Context context, List<IndexDetail_ct> indexDetailCts) {
        this.context = context;
        this.indexDetailCts = indexDetailCts;
        hasSIMCard = new CSIMCardUtil_ct(context).isHasSIMCard();
    }

    @Override
    public int getCount() {
        return indexDetailCts.size();
    }

    @Override
    public IndexDetail_ct getItem(int position) {
        return indexDetailCts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.useful_tel_detail_ct, null);
            viewHolder.useful_index_name = (TextView) view.findViewById(R.id.useful_index_name);
            viewHolder.useful_detail_name = (TextView) view.findViewById(R.id.useful_detail_name);
            viewHolder.useful_detail = (TextView) view.findViewById(R.id.useful_detail);
            viewHolder.useful_tel_line = (TextView) view.findViewById(R.id.useful_tel_line);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (position == getPositionForSection(indexDetailCts.get(position).getIndex())) {
            viewHolder.useful_index_name.setVisibility(View.VISIBLE);
            viewHolder.useful_tel_line.setVisibility(View.VISIBLE);
            viewHolder.useful_index_name.setText(indexDetailCts.get(position).getIndex());
        } else {
            viewHolder.useful_index_name.setVisibility(View.GONE);
            viewHolder.useful_tel_line.setVisibility(View.GONE);
        }

        viewHolder.useful_detail_name.setText(indexDetailCts.get(position).getTitle());
        String DetailsString = indexDetailCts.get(position).getDetail().replace(",", "\t\t\t\t").replace("=", "\n\n");
        viewHolder.useful_detail.setText(DetailsString);

        {
            //viewHolder.useful_detail.setAutoLinkMask(Linkify.ALL);
            CharSequence content = viewHolder.useful_detail.getText();
            SpannableStringBuilder builder = SpannableStringBuilder.valueOf(content);
            // listen and get the spans
            URLSpan[] spans = builder.getSpans(0, content.length(), URLSpan.class);
            if (spans != null && spans.length > 0) {
                int start = 0;
                int end = 0;
                for (URLSpan span : spans) {
                    start = builder.getSpanStart(span);
                    end = builder.getSpanEnd(span);
                    // to replace each link span with customized ClickableSpan
                    builder.removeSpan(span);
                    builder.setSpan(new MyURLSpan(context, span.getURL().toString(), viewHolder.useful_detail_name.getText().toString()),
                            start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    
                }
            }
            //viewHolder.useful_detail.setAutoLinkMask(0);
            viewHolder.useful_detail.setText(builder);
        }
        return view;
    }

    final static class ViewHolder {
        TextView useful_index_name;
        TextView useful_detail_name;
        TextView useful_detail;
        TextView useful_tel_line;
    }

    class MyURLSpan extends ClickableSpan {
        private Context mContext = null;
        private String mUrl = "";
        private String contactName;

        public MyURLSpan(Context context, String url, String contactName) {
            this.mContext = context;
            this.mUrl = url;
            this.contactName = contactName;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);//去除下划线,
        }

        @Override
        public void onClick(View widget) {
            String title = "";
            final String[] mailtoOrTel = mUrl.split(":");
            if (mailtoOrTel[0].equals("tel")) {
                title = "拨打确认";
            } else {
                title = "打开邮箱";
            }
            //拨打电话
            final MaterialDialog dialog = new MaterialDialog(mContext);
            dialog.setTitle(title);
            dialog.setMessage(contactName + "\n" +
                    mailtoOrTel[1]);
            dialog.setPositiveButton("确认", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    if (hasSIMCard) {

                        if (mailtoOrTel[0].equals("tel")) {
                            Intent intent = new Intent().setData(Uri.parse(mUrl));
                            intent.setAction(Intent.ACTION_CALL);
                            mContext.startActivity(intent);
                        } else {
                            Uri uri = Uri.parse(mUrl);
                            Intent intent4 = new Intent(Intent.ACTION_SENDTO, uri);
                            mContext.startActivity(intent4);
                        }

                    } else {
                        CommonUtil.showToast(mContext, "SIM卡未插入或暂时不可用!");
                    }
                }
            });
            dialog.setNegativeButton("取消", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    }

    /**
     *
     */
    public int getPositionForSection(String section) {
        for (int i = 0; i < getCount(); i++) {
            String indexStr = indexDetailCts.get(i).getIndex();
            if (indexStr.equals(section)) {
                return i;
            }
        }
        return -1;
    }
}
